import router from '../router/index.js'
import { MessageBox } from 'element-ui';
// let loginPath = 'https://usercenter.bsedu.com/login'
// let loginPath = 'https://test-usercenter.bsedu.com/login'
const vm = new Vue()
const loginPath = vm.$loginUrl

export const setstorage = (key, value) => {
  // var curTime = new Date().getTime()
  // localStorage.setItem(key,JSON.stringify({data:value,time:curTime}))
  localStorage.setItem(key, JSON.stringify(value))
}

export const getstorage = (key, exp) => {
  // var data = localStorage.getItem(key)
  var data = localStorage.getItem('token')
  var dataObj = JSON.parse(data)
  if (new Date().getTime() - dataObj.startTime > exp) {
    localStorage.removeItem('token')
    MessageBox.confirm('当前登录过期，请登录后下载', '提示', {
      confirmButtonText: '登录',
      cancelButtonText: '取  消',
      type: 'warning'
    }).then(() => {
      let path = router.path
      sessionStorage.setItem('urlPath', path)
      // router.push('/login')
      window.location.href = loginPath
    })
  } else {
    return dataObj
  }
}
